package com.st.shopadmin.util;

import java.io.IOException;
import java.text.SimpleDateFormat;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonUtil {
    private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);
    private static ObjectMapper objectMapper = new ObjectMapper();
    static {
        //对象所有字段全部列入
        objectMapper.setSerializationInclusion(Inclusion.ALWAYS);
        //取消默认转换TIMESTAMPS形式
        objectMapper.configure(SerializationConfig.Feature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
        //忽略空bean转json错误
        objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,false);
        //叟有日期格式统一yyyy-MM-dd HH:mm:ss
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        //忽略在json串中存在java对象中不存在防止报错
        objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false);
    }

    public static <T> String objToString(T obj){
        if(obj == null){
            return  null;
        }
        try {
            return obj instanceof String ? (String)obj :objectMapper.writeValueAsString(obj);
        } catch (IOException e) {
            logger.warn("pase object to String error");
            return null;
        }
    }

    public static <T> String objToStringPretty(T obj){
        if(obj == null){
            return  null;
        }
        try {
            return obj instanceof String ? (String)obj :objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (IOException e) {
            logger.warn("pase object to String error");
            return null;
        }
    }

    public static <T> T stringToObj(String str,Class<T> clazz){
        if(StringUtils.isEmpty(str) || clazz == null){
            return  null;
        }
        try {
            return clazz.equals(String.class)? (T)str:objectMapper.readValue(str,clazz);
        } catch (IOException e) {
            logger.warn("pase String to object error");
            return null;
        }
    }

    public static <T> T stringToObj(String str, TypeReference<T> tTypeReference){
        if(StringUtils.isEmpty(str) || tTypeReference == null){
            return  null;
        }
        try {
            return (T)(tTypeReference.getType().equals(String.class)? str:objectMapper.readValue(str,tTypeReference));
        } catch (IOException e) {
            logger.warn("pase String to object error");
            return null;
        }
    }


    public static <T> T stringToObj(String str, Class<?> collectionClass,Class<?> ... elementClasses){
        JavaType javaType =  objectMapper.getTypeFactory().constructParametricType(collectionClass,elementClasses);
        try {
            return objectMapper.readValue(str,javaType);
        } catch (IOException e) {
            logger.warn("pase String to object error");
            return null;
        }
    }



}
